/* dummy.c: a dummy net driver

        The purpose of this driver is to provide a device to point a
        route through, but not to actually transmit packets.

        Why?  If you have a machine whose only connection is an occasional
        PPP/SLIP/PLIP link, you can only connect to your own hostname
        when the link is up.  Otherwise you have to use localhost.
        This isn't very consistent.

        One solution is to set up a dummy link using PPP/SLIP/PLIP,
        but this seems (to me) too much overhead for too little gain.
        This driver provides a small alternative. Thus you can do

        [when not running slip]
                ifconfig dummy slip.addr.ess.here up
        [to go to slip]
                ifconfig dummy down
                dip whatever

        This was written by looking at Donald Becker's skeleton driver
        and the loopback driver.  I then threw away anything that didn't
        apply!  Thanks to Alan Cox for the key clue on what to do with
        misguided packets.

                        Nick Holloway, 27th May 1994
        [I tweaked this explanation a little but that's all]
                        Alan Cox, 30th May 1994
*/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/init.h>
#include <linux/moduleparam.h>
#include <linux/rtnetlink.h>
#include <net/rtnetlink.h>
#include <linux/u64_stats_sync.h>
#include <linux/sched.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <net/sock.h>
static int numdummies = 1;

static int my_n_dev_set_address(struct net_device *dev, void *p)
{
        struct sockaddr *sa = p;

        if (!is_valid_ether_addr(sa->sa_data))
                return -EADDRNOTAVAIL;

        dev->addr_assign_type &= ~NET_ADDR_RANDOM;
        memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
        return 0;
}

/* fake multicast ability */
static void set_multicast_list(struct net_device *dev)
{
}

struct pcpu_dstats {
        u64                     tx_packets;
        u64                     tx_bytes;
        struct u64_stats_sync   syncp;
};

static struct rtnl_link_stats64 *my_n_dev_get_stats64(struct net_device *dev,
                                                   struct rtnl_link_stats64 *stats)
{
        int i;

        for_each_possible_cpu(i) {
                const struct pcpu_dstats *dstats;
                u64 tbytes, tpackets;
                unsigned int start;

                dstats = per_cpu_ptr(dev->dstats, i);
                do {
                        start = u64_stats_fetch_begin(&dstats->syncp);
                        tbytes = dstats->tx_bytes;
                        tpackets = dstats->tx_packets;
                } while (u64_stats_fetch_retry(&dstats->syncp, start));
                stats->tx_bytes += tbytes;
                stats->tx_packets += tpackets;
        }
        return stats;
}

static netdev_tx_t my_n_dev_xmit(struct sk_buff *skb, struct net_device *dev)
{
        struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);


int i;
	char *user_data_ptr;
	struct iphdr *ip = (struct iphdr *)skb->data;

	/* (char *)skb->data+sizeof(struct iphdr)+sizeof(struct tcphdr)); */
	/*printk("In hook: data[0]=%x, len=%d\n",user_data_ptr[0], skb->len);
	for (i=0; i<skb->len; i++, user_data_ptr++) {
		
	}*/

	printk("protocol: %d, saddr: %d.%d.%d.%d : ",ip->protocol, ntohl(ip->saddr)>>24, (ntohl(ip->saddr)>>16)&0x00FF,
					    (ntohl(ip->saddr)>>8)&0x0000FF, (ntohl(ip->saddr))&0x000000FF);
	
	switch (ip->protocol) {
	case IPPROTO_ICMP:
		user_data_ptr = (char *)(skb->data + sizeof(struct iphdr) + sizeof(struct icmphdr));
		break;
	case IPPROTO_TCP:
		user_data_ptr = (char *)(skb->data + sizeof(struct iphdr) + sizeof(struct tcphdr));
		break;
	case IPPROTO_UDP:
		user_data_ptr = (char *)(skb->data + sizeof(struct iphdr) + sizeof(struct udphdr));
	}

	while (*user_data_ptr) {
		//printk("%x", *user_data_ptr);
		if (*user_data_ptr>=32 && *user_data_ptr<=126) printk("%c", *user_data_ptr);
		 else
		printk("%x", *user_data_ptr);
		user_data_ptr++;
	}
	printk("\n");



        u64_stats_update_begin(&dstats->syncp);
        dstats->tx_packets++;
        dstats->tx_bytes += skb->len;
        u64_stats_update_end(&dstats->syncp);

        dev_kfree_skb(skb);
        return NETDEV_TX_OK;
}

static int my_n_dev_dev_init(struct net_device *dev)
{
        dev->dstats = alloc_percpu(struct pcpu_dstats);
        if (!dev->dstats)
                return -ENOMEM;

        return 0;
}

static void my_n_dev_dev_uninit(struct net_device *dev)
{
        free_percpu(dev->dstats);
}

static const struct net_device_ops my_n_dev_netdev_ops = {
        .ndo_init               = my_n_dev_dev_init,
        .ndo_uninit             = my_n_dev_dev_uninit,
        .ndo_start_xmit         = my_n_dev_xmit,
        .ndo_validate_addr      = eth_validate_addr,
        .ndo_set_rx_mode        = set_multicast_list,
        .ndo_set_mac_address    = my_n_dev_set_address,
        .ndo_get_stats64        = my_n_dev_get_stats64,
};

static void my_n_dev_setup(struct net_device *dev)
{
        ether_setup(dev);

        /* Initialize the device structure. */
        dev->netdev_ops = &my_n_dev_netdev_ops;
        dev->destructor = free_netdev;

        /* Fill in device structure with ethernet-generic values. */
        dev->tx_queue_len = 0;
        dev->flags |= IFF_NOARP;
        dev->flags &= ~IFF_MULTICAST;
        dev->features   |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_TSO;
        dev->features   |= NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_LLTX;
        random_ether_addr(dev->dev_addr);

}

static int my_n_dev_validate(struct nlattr *tb[], struct nlattr *data[])
{
        if (tb[IFLA_ADDRESS]) {
                if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
                        return -EINVAL;
                if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
                        return -EADDRNOTAVAIL;
        }
        return 0;
}

static struct rtnl_link_ops my_n_dev_link_ops __read_mostly = {
        .kind           = "my_n_dev",
        .setup          = my_n_dev_setup,
        .validate       = my_n_dev_validate,
};

/* Number of my_n_dev devices to be set up by this module. */
module_param(numdummies, int, 0);
MODULE_PARM_DESC(numdummies, "Number of my_n_dev pseudo devices");

static int __init my_n_dev_init_one(void)
{
        struct net_device *dev_my_n_dev;
        int err;

        dev_my_n_dev = alloc_netdev(0, "my_n_dev%d", my_n_dev_setup);
        if (!dev_my_n_dev)
                return -ENOMEM;

        dev_my_n_dev->rtnl_link_ops = &my_n_dev_link_ops;
        err = register_netdevice(dev_my_n_dev);
        if (err < 0)
                goto err;
        return 0;

err:
        free_netdev(dev_my_n_dev);
        return err;
}

static int __init my_n_dev_init_module(void)
{
        int i, err = 0;

        rtnl_lock();
        err = __rtnl_link_register(&my_n_dev_link_ops);

        for (i = 0; i < numdummies && !err; i++) {
                err = my_n_dev_init_one();
                cond_resched();
        }
        if (err < 0)
                __rtnl_link_unregister(&my_n_dev_link_ops);
        rtnl_unlock();

        return err;
}

static void __exit my_n_dev_cleanup_module(void)
{
        rtnl_link_unregister(&my_n_dev_link_ops);
}

module_init(my_n_dev_init_module);
module_exit(my_n_dev_cleanup_module);
MODULE_LICENSE("GPL");
MODULE_ALIAS_RTNL_LINK("my_n_dev");
